//
//  MainOptionView.m
//  BigTyre
//
//  Created by 田涛 on 2017/3/25.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "MainOptionView.h"
#import "HomeModel.h"

#import <SDWebImage/UIImageView+WebCache.h>

@interface MainOptionView ()
@property (nonatomic, copy) NSArray *dataArr;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *image;
@end

@implementation MainOptionView

- (instancetype)initWithFrame:(CGRect)frame data:(NSArray *)data {
    if (self = [super initWithFrame:frame]) {
        _dataArr = data;
        [self initScrollView];
    }
    return self;
}

- (void)initScrollView {
    CGFloat scrollWidth = [UIScreen mainScreen].bounds.size.width / 2;
    CGFloat scrollHeight = self.bounds.size.height;
    CGFloat margin = 10;
    UIScrollView *scrollV = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollV.contentSize = CGSizeMake(scrollWidth *_dataArr.count + margin *_dataArr.count, scrollHeight);
    for (int i = 0; i < _dataArr.count; i++) {
        _image = [[UIImageView alloc] initWithFrame:CGRectMake(margin *i + margin +scrollWidth * i, 0, scrollWidth, scrollHeight)];
        HomeModel_Option *model_o = _dataArr[i];
        [_image sd_setImageWithURL:[NSURL URLWithString:model_o.img] placeholderImage:nil];
        _image.userInteractionEnabled = YES;
        _image.layer.cornerRadius = 5;
        _image.layer.masksToBounds = YES;
        _image.layer.borderWidth = 0.2;
        _image.tag = 100 + i;
        _image.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [scrollV addSubview:self.image];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        tap.numberOfTapsRequired = 1;
        [_image addGestureRecognizer:tap];
    }
    [self addSubview:scrollV];
    _scrollView = scrollV;
}


- (void)click:(id )sender {
    
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
    UIImageView *img = (UIImageView *)tap.view;
    int tagCliked = (int)img.tag;
    
    switch (tagCliked) {
        case 100:
            if (self.pushBlock != nil) {
                HomeModel_Option *model = _dataArr[0];
                self.pushBlock(model.sID);
            }
            break;
        case 101:
            if (self.pushBlock != nil) {
                HomeModel_Option *model = _dataArr[1];
                self.pushBlock(model.sID);
            }
            break;
        case 102:
            if (self.pushBlock != nil) {
                HomeModel_Option *model = _dataArr[2];
                self.pushBlock(model.sID);
            }
            break;
        case 103:
            if (self.pushBlock != nil) {
                HomeModel_Option *model = _dataArr[3];
                self.pushBlock(model.sID);
            }
            break;
            
        default:
            break;
    }
    
}

@end
